

<template>
  <div id="app">
    <!-- 查询表单 -->
<!--    欢迎{{this.user.username}}登录-->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="设备类型名称">
        <el-input v-model="formInline.name" placeholder="请输入设备类型名称"></el-input>
      </el-form-item>

      <el-form-item label="品牌">
        <el-input v-model="formInline.brand" placeholder="请输入品牌"></el-input>
      </el-form-item>

      <el-form-item label="状态">
        <el-select v-model="formInline.status" placeholder="全部" clearable>
          <el-option label="启用" value="1"></el-option>
          <el-option label="禁用" value="2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="findPage()">查询</el-button>
      </el-form-item>
    </el-form>

    <el-button type="info" @click="toAdd()" plain>添加</el-button>
<!--    <el-button type="primary" @click="daochu()">导出</el-button>-->

    <!--表格用来展示数据-->
    <el-table
        :data="tableData"
        border
        style="width: 100%">
      <el-table-column
          prop="id"
          label="序号"
      >
      </el-table-column>

      <el-table-column
          prop="name"
          label="设备类型名称"
      >
        <template slot-scope="scope">
          <el-button type="text" @click="tc(scope.row.id)">{{scope.row.name}}</el-button>
        </template>
      </el-table-column>

      <el-table-column
          prop="brand"
          label="品牌"
      >
      </el-table-column>

      <el-table-column
          prop="xinghao"
          label="型号"
      >
      </el-table-column>

      <el-table-column
          prop="guige"
          label="规格"
      >
      </el-table-column>

      <el-table-column
          prop="ctime"
          label="创建时间"
      >
      </el-table-column>

      <el-table-column
          prop="status"
          label="状态"
      >
        <template slot-scope="scope">
          {{scope.row.status==1?"启用":"禁用"}}
        </template>
      </el-table-column>

      <el-table-column
          label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="toUpdate(scope.row.id)" size="mini">修改</el-button>
          <el-button type="text" v-if="scope.row.status==1" @click="zhuangtai(scope.row.id,2)" size="mini">禁用</el-button>
          <el-button type="text" v-if="scope.row.status==2" @click="zhuangtai(scope.row.id,1)" size="mini">启用</el-button>
          <el-button type="text" @click="del(scope.row.id)" size="mini">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!-- 分页展示数据-->
    <el-pagination
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[3, 6, 9, 12]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <!--添加弹窗-->
    <el-dialog
        title="添加"
        :visible.sync="dialogVisible"
        width="30%">

      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

        <el-form-item label="设备类型名称:" prop="name">
          <el-input v-model="ruleForm.name" placeholder="请输入"></el-input>
        </el-form-item>

        <el-form-item label="品牌:" prop="name">
          <el-input v-model="ruleForm.brand" placeholder="请输入"></el-input>
        </el-form-item>

        <el-form-item label="型号:" prop="name">
          <el-input v-model="ruleForm.xinghao" placeholder="请输入"></el-input>
        </el-form-item>

        <el-form-item label="状态:" prop="name">
          <el-tooltip :content="'启用是1禁用是2: ' + ruleForm.status" placement="top">
            <el-switch
                v-model="ruleForm.status"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="2">
            </el-switch>
          </el-tooltip>
        </el-form-item>

        <el-form-item label="规格:" prop="name">
          <textarea v-model="ruleForm.guige" placeholder="请输入"></textarea>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="add('ruleForm')" v-text="ruleForm.id==null?'添加':'修改'"></el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!--点击弹窗-->
    <el-dialog
        title="查看"
        :visible.sync="dialogVisible2"
        width="30%">

      <el-form :model="ruleForm2" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

        <el-form-item label="设备类型名称:" prop="name">
          {{ruleForm2.name}}
        </el-form-item>

        <el-form-item label="品牌:" prop="name">
          {{ruleForm2.brand}}
        </el-form-item>

        <el-form-item label="型号:" prop="name">
          {{ruleForm2.xinghao}}
        </el-form-item>

        <el-form-item label="状态:" prop="name">
          {{ruleForm2.status==1?"启用":"禁用"}}
        </el-form-item>

        <el-form-item label="规格:" prop="name">
          {{ruleForm2.guige}}
        </el-form-item>

        <el-button type="primary" @click="guanbi()">关闭</el-button>
      </el-form>
    </el-dialog>

  </div>
</template>

<style >

</style>

<script>
// @ is an alias to /src
import axios from "axios"

export default {
  data(){
    return{
      /*
        data:就是用来定义数据的要用什么就自己定义什么
        []:数组，集合
        {}:对象(里面有属性)
         */
      tableData:[],
      pageNum:1,
      pageSize:3,
      total:0,
      formInline:{},
      user:{},
      roomList:[],
      dialogVisible:false,
      dialogVisible2:false,
      tx:[],
      ruleForm:{},
      ruleForm2:{},
      imageUrl:"",
    }
  },
  methods:{
    /*！！！！！！！！！！！
    methods:这个里面定义的全部都是方法
    现在是spring boot+vue 框架
    不一样的就是在 从vue框架里面的methods里面
    通过方法去请求路径(请求的路径就是后台的路径)
    拿到数据放到下面的created()里面来加载数据才能在页面展示

    ***html使用ajxa 请求controller  controller把数据返回给前端 不再去跳转页面了***

     */
    findPage(){ //查询所有加分页参数
      this.axios.post(`http://localhost:9001/sblx/findPage?pageNum=${this.pageNum}&pageSize=${this.pageSize}`,this.formInline).then(res=>{
        console.log(res.data);
        this.tableData=res.data.data.records;
        this.total=res.data.data.total;
        if (this.pageNum>res.data.data.pages){
          this.pageNum=res.data.data.pages;
          this.findPage();
        }
      })
    },
    handleCurrentChange(val) {//分页方法
      this.pageNum=val;
      this.findPage();
    },
    toAdd(){ //往添加对话框里清空数据
      this.dialogVisible=true;
      this.ruleForm={};
    },
    add(formName) { //添加方法
      this.$refs[formName].validate((valid) => {
        if (valid) {//满足校验
          //通过ajax把数据保存到后端
          this.ruleForm.tx=this.tx.toString();
          axios.post(`http://localhost:9001/sblx/add`,this.ruleForm).then(res=>{
            if (res.data.code==200){
              this.$message.success("添加成功");
            }else{
              this.$message.error("添加失败");
            }
            this.dialogVisible=false;
            this.findPage();
          })
        } else {//不满足校验
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) { //重置按钮点击可以重置
      this.$refs[formName].resetFields();
    },
    toUpdate(id){ //修改回显数据方法
      axios.get(`http://localhost:9001/sblx/findById?id=${id}`).then(res=>{
        this.ruleForm=res.data.data;
        this.dialogVisible=true;
      })
    },
    del(id){ //删除方法
      this.$confirm("确定要删除吗?一旦删除,该条信息将不能恢复。", '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.get(`http://localhost:9001/sblx/deleteById?id=${id}`).then(res=>{
          if (res.data.code==1){
            this.$message.success("删除成功");
          }else{
            this.$message.error("删除失败！");
          }
          this.findPage();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    zhuangtai(id,status){ //状态修改方法
      axios.get(`http://localhost:9001/sblx/zhuangtai?id=${id}&status=${status}`).then(res=>{
        if(res.data.code==200){
          this.$message.success(res.data.data);
          this.findPage();
        }else{
          this.$message.error(res.data.data);
        }
      })
    },
    tc(id){
      this.dialogVisible2=true;
      this.axios.get(`http://localhost:9001/sblx/findById?id=${id}`).then(res=>{
        this.ruleForm2=res.data.data;
      })
    },
    guanbi(){
      this.dialogVisible2=false;
    },
  },
  created(){
    /*
    ！！！！！！！！！！注意created()：
    这个里面才是可以把数据展示到页面的方法
    思考那个方法是要把数据放到前台 就在这个里面写那个方法
     */
    // var userDB=sessionStorage.getItem("userDB"); //从login.html页面的session域中拿到的数据
    // if (userDB==null){
    //   location.href="#/login";
    // }
    // this.user=JSON.parse(userDB);//动态获取显示登录人的数据 因为从session域中拿出来所以要转换数据类型
    this.findPage();//展示所有的数据
    // this.xialaList();//动态获取下拉框的数据
  }
}
</script>
